<?php

class DepartmentController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$departments = Department::paginate(10);
		return View::make('departments.index')->with(array('departments' => $departments));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$department = new Department;
		return View::make('departments.edit')->with(array('department' => $department, 'method' => 'post'));
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		// Get all the inputs
        // id is used for login, username is used for validation to return correct error-strings
        $userdata = array(
            'short_department_name' => Input::get('short_department_name'),
            'department_name' => Input::get('department_name'),
			'department_description' => Input::get('department_description'),
			'pdf_password' => Input::get('pdf_password'),
			'templates' => explode(', ', rtrim(trim(Input::get('templates')), ','))
        );
		
        // Declare the rules for the form validation.
        $rules = array(
            'short_department_name'  => 'Required|unique:departments',
            'department_name'  => 'Required',
            'pdf_password'  => 'Required|min:8',
            'templates'  => 'Required|array|exists:templates,template_name'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes()) {
			
			$templates = Template::whereIn('template_name', $userdata['templates']);
			$department = Department::create(Input::all());
						
			if ($department->id > 0)
			{
				$department->templates()->sync($templates->lists('id'));
				
                // Redirect to homepage
                return Redirect::to('departments')->with('success', 'New department successfully added');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('departments/edit')->withErrors(array('message' => 'Opps something wrong!!'));
				
			}
		}
		
        // Something went wrong.
        return Redirect::to('departments/create')->with(array('method' => 'post'))->withErrors($validator)->withInput(Input::all());
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$department = Department::findOrFail($id);
		return View::make('departments.show')->with('department', $department);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$department = Department::findOrFail($id);
		return View::make('departments.edit')->with(array('department' => $department, 'method' => 'put'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		// Retrieve current user
		$department = Department::find($id);
	
		// Get all the inputs
        // id is used for login, username is used for validation to return correct error-strings
        $userdata = array(
            'short_department_name' => Input::get('short_department_name'),
            'department_name' => Input::get('department_name'),
			'department_description' => Input::get('department_description'),
			'pdf_password' => Input::get('pdf_password'),
			'templates' => explode(', ', rtrim(trim(Input::get('templates')), ','))
        );

        // Declare the rules for the form validation.
        $rules = array(
            'short_department_name'  => 'Required',
            'department_name'  => 'Required',
            'pdf_password'  => 'Required|min:8',
            'templates'  => 'Required|array|exists:templates,template_name'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes()) {
			
			$templates = Template::whereIn('template_name', $userdata['templates']);
			
			if ($department->update(Input::all()))
			{
				$department->templates()->sync($templates->lists('id'));
				
                // Redirect to homepage
                return Redirect::to('departments')->with('success', 'Department successfully Updated');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('departments/edit')->withErrors(array('message' => 'Opps something wrong!!'));
			}
		}
		
        // Something went wrong.
        return Redirect::to('departments/'.$department->id.'/')->with(array('method' => 'post'))->withErrors($validator)->withInput(Input::all());
	}
	
	/**
	 * Show the form for delete the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function delete($id)
	{
		$department = Department::find($id);
		return View::make('departments.edit')->with(array('department' => $department, 'method' => 'delete'));
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Retrieve current Department
		$department = Department::find($id);
		
		User::where('department_id', '=', $department->id)->delete();
		$department->templates()->sync(array());
		$department->permissions()->sync(array());
		$department->delete();
		
		return Redirect::to('departments')->with('success', 'Successfully deleted department!');
	}


}
